--
local sdtp_xdr=Proto.new("sdtp_xdr", "sdtp body xdr")
--dofile(USER_DIR.."../../.local/lib/wireshark/plugins/uuhdr.lua")
--dofile(USER_DIR.."../../.local/lib/wireshark/plugins/uuhdr_ext.lua")
--dofile(USER_DIR.."../../.local/lib/wireshark/plugins/x2hdr.lua")
--
loadfile(USER_DIR.."../../.local/lib/wireshark/plugins/uuhdr.lua")
loadfile(USER_DIR.."../../.local/lib/wireshark/plugins/uuhdr_ext.lua")
loadfile(USER_DIR.."../../.local/lib/wireshark/plugins/x2hdr.lua")

local cardtype_list = 
{
    [0x01] = "FE/GE",
    [0x02] = "E1",
    [0x03] = "ATM",
    [0x04] = "STM",
    [0x05] = "BSC",
    [0x06] = "LTE_SOFT"
}

local netele_list = 
{
    [0x01] = "ENODEB",
    [0x02] = "MME",
    [0x03] = "SGW",
    [0x04] = "PGW",
    [0x05] = "PCRF",
    [0x06] = "HSS"
}

local iface_list = 
{
    [0x01] = "Uu",
    [0x02] = "Uu-extend",
    [0x03] = "x2",
}

local dir_list = 
{
    [0x00] = "None",
    [0x01] = "Tx",
    [0x02] = "Rx"
}

-- 长度
local xdr_len = ProtoField.uint16("sdtp_xdr.len", "len", base.DEC)
-- 通用头部分
local ver = ProtoField.uint8("sdtp_xdr.version", "ver", base.HEX)
local cardtype = ProtoField.uint8("sdtp_xdr.cardtype", "cardtype", base.HEX, cardtype_list)
local time1 = ProtoField.uint32("sdtp_xdr.time_sec", "time_sec", base.DEC)
local time2 = ProtoField.uint32("sdtp_xdr.time_nsec", "time_nsec", base.DEC)
local netele = ProtoField.uint8("sdtp_xdr.net_element", "net_element", base.DEC, netele_list)
local iface = ProtoField.uint8("sdtp_xdr.interface", "interface", base.HEX, iface_list)
local dir = ProtoField.uint8("sdtp_xdr.dir", "dir", base.HEX, dir_list)
-- 专用包头部分
local xdr_payload = ProtoField.string("sdtp_xdr.payload", "Payload")

---
sdtp_xdr.fields = {xdr_len, ver, cardtype, time1, time2, netele, iface, dir, xdr_payload}

function sdtp_xdr.dissector(buf, pinfo, tree)
    local offset = 0
    -- xdr_len
    local len = buf(offset, 2):uint()
    xdrtree = tree:add(sdtp_xdr, buf(0, len + 2), "xdr tree")
    left = len + 2

    xdrtree:add(xdr_len, buf(offset, 2))
    offset = offset + 2;
    left = left - 2;

    -- ver
    xdrtree:add(ver, buf(offset, 1))
    offset = offset + 1;
    left = left - 1;

    -- cardtype
    xdrtree:add(cardtype, buf(offset, 1))
    offset = offset + 1;
    left = left - 1;

    -- time1
    xdrtree:add(time1, buf(offset, 4))
    offset = offset + 4;
    left = left - 4;

    -- time2
    xdrtree:add(time2, buf(offset, 4))
    offset = offset + 4;
    left = left - 4;

    --net element 
    xdrtree:add(netele, buf(offset, 1))
    offset = offset + 1;
    left = left - 1;

    --iface
    local interface = buf(offset,1):uint()
    xdrtree:add(iface, buf(offset, 1))
    offset = offset + 1;
    left = left - 1;

    --dir
    xdrtree:add(dir, buf(offset, 1))
    offset = offset + 1;
    left = left - 1;

    xdrtree:add(xdr_payload, buf(offset, left), left.." bytes")
    if (interface == 0x01) then
        local diss = Dissector.get("uuhdr")
        Dissector.call(diss, buf(offset, left):tvb(), pinfo, xdrtree)
    elseif (interface == 0x02 ) then
        local diss = Dissector.get("uuhdr_ext")
        Dissector.call(diss, buf(offset, left):tvb(), pinfo, xdrtree)
    elseif(interface == 0x03) then
        local diss = Dissector.get("x2hdr")
        Dissector.call(diss, buf(offset, left):tvb(), pinfo, xdrtree)
    end

    return len + 2 
end

-- 把解析器注册给wireshark
--DissectorTable.new("sdtp_xdr"):add(1, sdtp_xdr)
--Dissector("sdtp_xdr", sdtp_xdr)
